package tester;

import java.io.*;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: aleck
 * Date: 2007-9-21
 * Time: 14:40:17
 */
public class Problem {
  public static final String configFilePostfix = "config.txt";
  public static final String defaultCasesCount = "10";
  public static final String defaultTimeout = "1000";
  public static final String defaultComparator = "tolerant";
  public final File configFile;
  public final File baseDir;
  public final String executableName;
  public final String cSourceFileName;
  public final String cppSourceFileName;
  // name is in upperCase
  public final String name;
  public final int totalScore;
  public final int casesCount;
  public final int timeout;
  public final Comparator comparator;

  public Problem(File baseDir)
      throws InternalErrorException {
    this.name = baseDir.getName().toUpperCase();
    this.executableName = baseDir.getName() + ".exe";
    this.cSourceFileName = baseDir.getName() + ".c";
    this.cppSourceFileName = baseDir.getName() + ".cpp";
    this.baseDir = baseDir;
    configFile =
        new File(baseDir.getPath() + File.separator + configFilePostfix);
    if (configFile.exists()) {
      Properties properties = new Properties();
      try {
        InputStream configFileInputStream = new FileInputStream(configFile);
        properties.load(configFileInputStream);
      } catch (IOException e) {
        e.printStackTrace();
      }
      casesCount =
          Integer.parseInt(properties.getProperty("cases.count", "10"));
      timeout = Integer.parseInt(properties.getProperty("timeout", "1000"));
      String compString = properties.getProperty("comparator", "tolerant");
      totalScore =
          Integer.parseInt(properties.getProperty("totalScore", "100"));
      // TODO: add other types of comparators
      if (compString.equals("tolerant")) {
        comparator = new TolerantComparator();
      } else {
        // by default
        comparator = new TolerantComparator();
      }
    } else {
      createDefaultConfigFile();
      casesCount = 10;
      timeout = 1000;
      totalScore = 100;
      comparator = new TolerantComparator();
    }
  }

  public String getCaseInput(int index) {
    if (index < 0 || index >= casesCount) return null;
    else return baseDir.getAbsolutePath() + File.separator + name + index + ".in";
  }

  public String getCaseOutput(int index) {
    if (index < 0 || index >= casesCount) return null;
    else return baseDir.getAbsolutePath() + File.separator + name + index + ".out";
  }

  private void createDefaultConfigFile() throws InternalErrorException {
    try {
      configFile.createNewFile();
      Properties properties = new Properties();
      properties.setProperty("cases.count", "" + countExistingTestCases());
      properties.setProperty("timeout", "1000");
      properties.setProperty("totalScore", "100");
      properties.setProperty("comparator", "tolerant");
      properties.store(new FileOutputStream(configFile), "");
    } catch (IOException e) {
      throw new InternalErrorException("can not create default config file: " +
          configFile.getName());
    }
  }

  private int countExistingTestCases() {
    int count = 0;
    File file = new File(baseDir + File.separator + name + count + ".in");
    while (file.exists()) {
      count++;
      file = new File(baseDir + File.separator + name + count + ".in");
    }
    return count;
  }

  public void saveToConfigFile() {
    // TODO
  }
}
